﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SMP.Client.MasterForm
{
    using System.Collections;
    using SMP.Windows;
    using SMP.Service;

    public partial class FrmNha_Cungcap : SMP.Windows.Forms.FrmBase
    {
        Nhaphang_Service _objNhaphang = new Nhaphang_Service();
        SMP.Domain.Nha_Cungcap nha_cungcap = new Domain.Nha_Cungcap();
        DataTable dtb_nha_cungcap = new DataTable();

        public FrmNha_Cungcap()
        {
            InitializeComponent();
        }

        #region DisplayInfo, ResetText, ChangeStatus, ClearDataBindings, DataBindingControl

        public override void DisplayInfo()
        {
            try
            {
                this.FormState = Windows.Forms.FormState.View;
                dtb_nha_cungcap = _objNhaphang.GetAll_Nha_Cungcap();
                dgNha_Cungcap.DataSource = dtb_nha_cungcap;

                ClearDataBindings();
                ResetText();
                ChangeStatus(true);
                if (gvNha_Cungcap.RowCount > 0)
                {
                    DataBindingControl();
                    item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                }
                else
                    item_Select.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            }
            catch (Exception ex)
            {
                Windows.Forms.Message.Show(ex.Message, ex.ToString());
            }
        }

        public override void ResetText()
        {
            txtTen_Nha_Cungcap.EditValue = null;
            txtDienthoai.EditValue = null;
            txtDiachi.EditValue = null;
            txtEmail.EditValue = null;
            txtGhichu.EditValue = null;
            txtTen_Nha_Cungcap.Focus();
        }

        public override void ChangeStatus(bool editTable = true)
        {
            txtTen_Nha_Cungcap.Properties.ReadOnly = editTable;
            txtDienthoai.Properties.ReadOnly = editTable;
            txtDiachi.Properties.ReadOnly = editTable;
            txtEmail.Properties.ReadOnly = editTable;
            txtGhichu.Properties.ReadOnly = editTable;
        }

        public override void ClearDataBindings()
        {
            txtTen_Nha_Cungcap.DataBindings.Clear();
            txtDienthoai.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtEmail.DataBindings.Clear();
            txtGhichu.DataBindings.Clear();
        }

        public override void DataBindingControl()
        {
            txtTen_Nha_Cungcap.DataBindings.Add("EditValue", dtb_nha_cungcap, ".Ten_nha_cungcap");
            txtDienthoai.DataBindings.Add("EditValue", dtb_nha_cungcap, ".Dienthoai");
            txtDiachi.DataBindings.Add("EditValue", dtb_nha_cungcap, ".Diachi");
            txtEmail.DataBindings.Add("EditValue", dtb_nha_cungcap, ".Email");
            txtGhichu.DataBindings.Add("EditValue", dtb_nha_cungcap, ".Ghichu");
        }

        #endregion


        #region PerformAdd, PerformEdit, PerformDelete, PerformCancel, PerformSave

        public override bool PerformAdd()
        {
            ClearDataBindings();
            ResetText();
            ChangeStatus(false);
            return true;
        }

        public override bool PerformEdit()
        {
            ChangeStatus(false);
            return true;
        }

        public override bool PerformDelete()
        {
            var del = false;
            if (_objNhaphang.Get_Nha_Cungcap_Check_Used_By_Id_Nha_Cungcap(gvNha_Cungcap.GetFocusedRowCellValue("Id_Nha_Cungcap")))
                Windows.Forms.Message.Show("Đơn vị tính " + txtTen_Nha_Cungcap.EditValue + " đã sử dụng, không thể xóa", "Thông Báo");

            if (Windows.Forms.Message.Show("Bạn có thực sự muốn xóa " + txtTen_Nha_Cungcap.EditValue + " ", "Thông Báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                del = _objNhaphang.Delete_Nha_Cungcap(gvNha_Cungcap.GetFocusedRowCellValue("Id_Nha_Cungcap"));
            if (del)
                DisplayInfo();
            return del;
        }

        public override bool PerformCancel()
        {
            ClearDataBindings();
            DisplayInfo();
            return true;
        }

        public override bool PerformSave()
        {
            var save = false;
            Hashtable hashtableControl = new Hashtable();
            hashtableControl.Add(txtTen_Nha_Cungcap, lbNha_Cungcap);
            if (!Windows.Utility.Validator.CheckNullFields(hashtableControl))
                return false;
            if (dtb_nha_cungcap != null && dtb_nha_cungcap.Rows.Count > 0)
            {
                DataRow[] row = dtb_nha_cungcap.Select(string.Format("Ten_nha_cungcap = '{0}'", txtTen_Nha_Cungcap.EditValue));
                if (row.Length > 0)
                {
                    Windows.Forms.Message.Show("Đơn vị tính " + txtTen_Nha_Cungcap.EditValue + " đã tồn tại.\nVui lòng xem lại", "Thông Báo", MessageBoxButtons.OK);
                    return false;
                }
            }
            nha_cungcap.Ten_Nha_Cungcap = txtTen_Nha_Cungcap.EditValue;
            nha_cungcap.Dienthoai = txtDienthoai.EditValue;
            nha_cungcap.Diachi = txtDiachi.EditValue;
            nha_cungcap.Email = txtEmail.EditValue;
            nha_cungcap.Ghichu = txtGhichu.EditValue;

            if (this.FormState == Windows.Forms.FormState.Add)
                save = _objNhaphang.Insert_Nha_Cungcap(nha_cungcap);
            else
                save = _objNhaphang.Update_Nha_Cungcap(nha_cungcap);
            if (save)
                DisplayInfo();


            return save;
        }

        #endregion

        #region Method

        public DataRow SelectedRow { set; get; }
        public override object PerformSelectOneObject()
        {
            SelectedRow = gvNha_Cungcap.GetDataRow(gvNha_Cungcap.FocusedRowHandle);

            if (SelectedRow == null)
            {
                Windows.Forms.Message.Show("Chưa chọn Nhà cung cấp.\nVui lòng chọn Đơn vị tính", "Thông Báo", MessageBoxButtons.OK);
                return null;
            }
            this.Close();
            this.Dispose();
            return true;
        }


        #endregion

      








    }
}